package tk.mybatis.springboot.ping;

import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.pingplusplus.Pingpp;
import com.pingplusplus.exception.APIConnectionException;
import com.pingplusplus.exception.APIException;
import com.pingplusplus.exception.AuthenticationException;
import com.pingplusplus.exception.ChannelException;
import com.pingplusplus.exception.InvalidRequestException;
import com.pingplusplus.exception.RateLimitException;
import com.pingplusplus.model.Charge;

import tk.mybatis.springboot.share.util.PropertiesUtil;

public class Ping {
	private static Logger LOGGER = LogManager.getLogger(Ping.class);
	private static String privateKeyFilePath;
	public static Charge pay(Integer money, String number, String ip, String channel, Map<String, Object> chargeParams, String sub){
		URL url=Ping.class.getResource("/rsa_private_key.pem");
    	privateKeyFilePath = url.toString();

    	privateKeyFilePath = privateKeyFilePath.replace("file:/", "");
    	try {
	    	privateKeyFilePath = URLDecoder.decode(privateKeyFilePath, "utf-8");
	    	if(!System.getProperty("os.name").toLowerCase().startsWith("win")){
	    		privateKeyFilePath = "/"+URLDecoder.decode(privateKeyFilePath, "utf-8");
	    	}
	        // 设置私钥路径，用于请求签名
	        Pingpp.privateKey = "MIIEpAIBAAKCAQEAzgNTKrfe1GINJkobg4qcq+ug/7lVzyT2Ft1tRls+1ItDCU3oVUgxPn27TrUD/RwrHVjd8JivgFbM+5oDCTTADTH6wQDj5IYbPYSlahTiVQQWXJ2oHaFpzdVIQGwdBGmmlQjuHTdVvJapqHDOqj8dhTZhmUAQysPBKFvSW/DU7lljM00Qw8sLuPNZoDIkbnPeSvYso63qegKXl7o+AuqnNxJ6gbPuSDSRtg+oLA+MPjn0JO/O1UtZo1REJDc/IboopvjBmMyLMmJvOcfZd+/HC+y1F4u9Dg2Vnwousj5MDCgfEzH9SJjp+3QdF9DifHMGoVkTB6XSWMesoR2MOYF+BQIDAQABAoIBAQCm47Sk/41JWiEy6of9ePXQpDKp1xc3lsaw4TIb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4lMFcU4cRjS/ZH1mUlUMdKM6bvRLNSp2S4Fb43KoK7BZmIUZ0YbhW4qQrxoJt1CemtCMoeAG+C8nr9pogezeaUVjzj23AbVmn609FcxrZPjOA==";
			Pingpp.apiKey = PropertiesUtil.getMessage("pingKay");
			chargeParams.put("order_no",  number);
			chargeParams.put("amount", money);//订单总金额, 人民币单位：分（如订单总金额为 1 元，此处请填 100）
			Map<String, String> app = new HashMap<String, String>();
			app.put("id", PropertiesUtil.getMessage("pingAppId"));
			chargeParams.put("app", app);
			chargeParams.put("channel",  channel);
			chargeParams.put("currency", "cny");
			chargeParams.put("client_ip",  ip);
			chargeParams.put("subject",  sub);
			chargeParams.put("body",  "e车险保");		
			return Charge.create(chargeParams);
		} catch (RateLimitException e) {
				// TODO Auto-generated catch block
			LOGGER.error(e.getMessage(),e);
		} catch (AuthenticationException e) {
			// TODO Auto-generated catch block
			LOGGER.error(e.getMessage(),e);
		} catch (InvalidRequestException e) {
			// TODO Auto-generated catch block
			LOGGER.error(e.getMessage(),e);
		} catch (APIConnectionException e) {
			// TODO Auto-generated catch block
			LOGGER.error(e.getMessage(),e);
		} catch (APIException e) {
			// TODO Auto-generated catch block
			LOGGER.error(e.getMessage(),e);
		} catch (ChannelException e) {
			// TODO Auto-generated catch block
			LOGGER.error(e.getMessage(),e);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			LOGGER.error(e.getMessage(),e);
		}
		return null;
	}
}
